// import Vue from 'vue'
import Vuex from 'vuex'
import {createStore} from 'vuex'
import {setToken, removeToken} from '~/uilti/auth'
import {getLoginAPI, getGetInfo, signOutApi} from "~/commonApi/api"
import Cookies from 'js-cookie'
import {signOut} from "../commonApi/api/login.js";
import {toast} from "../uilti/tool.js";

const store = createStore({
    state() {
        return {
            user: {},
            userId: '',
            logInStatus: false,
            keywords: '',
            // 侧边宽度
            asidewidth: '200px',
            playing: {},
            lyric: [],
            playStatus: {
                word: null
            },
            playTime: 0,
            isStatus: false,
            currenIndex: 0
        }
    },
    mutations: {
        handleasiedwidth(state) {
            state.asidewidth = state.asidewidth == '200px' ? '48px' : '200px'

        },
        SET_USERINfo(state, user) {
            state.user = user
            if (!state.logInStatus) {
                state.logInStatus = true
            }
        },
        SET_KEYWORDS(state, keywords) {
            state.keywords = keywords
        },
        SET_PLAY(state, playing) {
            state.playing = playing
        },
        //保存歌词
        SERLYRIC(state, lyric) {
            state.lyric = lyric
        },
        //修改播放歌词时间
        SET_PLAYTIME(state, playTime) {
            state.playTime = playTime
        },
        //修改索引
        SHOW_FECI(state, currenIndex) {
            state.currenIndex = currenIndex
        },
        //修改播放状态
        SET_IS_PLAY(state, isStatus) {
            state.isStatus = isStatus
        },
    },
    actions: {
        login({commit}, form) {
            return new Promise((resolve, reject) => {
                getLoginAPI(form).then(res => {
                    if (!res == 200) return
                    localStorage.setItem('id', res.account.id)
                    Cookies.set('token', res.token)
                    resolve()
                }).catch((err) => reject(err))
            })
        },
        //获取当前用户信息
        getinfo({commit}) {
            return new Promise((resolve, reject) => {
                getGetInfo(localStorage.getItem('id') || '').then((res) => {
                    commit('SET_USERINfo', res)
                    resolve(res)
                })
                    .catch((err) => reject(err))
            })
        },
        getplaying({commit}, playing) {
            commit('SET_PLAY', playing)
        },

        loginout({commit}) {
            //1.清除toekn
            const token = Cookies.get('token')
            const id = localStorage.getItem('id')
            const manner = sessionStorage.getItem('manner')
            if (token) {
                Cookies.remove('token')
            }
            if (id) {
                localStorage.removeItem('id')
            }
            if (manner) {
                sessionStorage.removeItem('manner')
            }
            return new Promise((resolve, reject) => {
                signOutApi().then((res) => {
                    resolve(res)
                })
                    .catch((err) => reject(err))
            })
            //2清除vuex状态
            commit('SET_USERINfo', {})
        },
        getkewords({commit,}, res) {
            commit('SET_KEYWORDS', res)
        }
    }
})
export default store